package com.sc.VenusQueryBeta.presenter.login;

import com.sc.VenusQueryBeta.contract.login.FirstLoginContract;
import com.sc.VenusQueryBeta.model.bean.login.EquipmentLoginBean;
import com.sc.VenusQueryBeta.model.bean.login.FirstLoginSubmintBean;
import com.sc.VenusQueryBeta.model.login.FirstLoginModel;

import io.reactivex.functions.Consumer;

/**
 * Created by SC-02 on 2018/9/19.
 * 登錄presenter 继承契约类的presenper
 */

public class FirstLoginPresenter extends FirstLoginContract.LoginPresenter {
    public static FirstLoginPresenter newInstance(){
        return new FirstLoginPresenter();
    }
    @Override
    protected FirstLoginContract.ILoginModel getModel() {
        return FirstLoginModel.newInstance();
    }

    @Override
    public void onStart() {

    }

    //第一次登錄發送驗證碼
    @Override
    public void getFirstBUserLoginCode(String clientUID, String deviceUID, String deviceType, String registerSmsnum) {
        mRxManager.register(mIModel.getFirstBUserLoginCode(clientUID,deviceUID,deviceType,registerSmsnum).subscribe(new Consumer<EquipmentLoginBean>() {

            @Override
            public void accept(EquipmentLoginBean equipmentLoginBean) throws Exception {
                if (mIView != null)
                    mIView.showFirstBUserLoginCodeSuccess(equipmentLoginBean);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                if (mIView != null) {
//                    if (mIView.isVisiable())
                    mIView.showToast("Network error.");
                    mIView.showNetworkError();
                }
            }
        }));
    }
    //第一次登录的页面确定后继续掉通过伺服器确认注册
    @Override
    public void firstSumbit(String id, String smsCode, String password, String clientUID, String deviceUID, String deviceType) {
        mRxManager.register(mIModel.firstSumbit(id,smsCode,password,clientUID,deviceUID,deviceType).subscribe(new Consumer<FirstLoginSubmintBean>() {
            @Override
            public void accept(FirstLoginSubmintBean firstLoginSubmintBean) throws Exception {
                if (mIView != null)
                    mIView.showFirstSumbitDataSuccess(firstLoginSubmintBean);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                if (mIView != null) {
//                    if (mIView.isVisiable())
                    mIView.showToast("Network error:"+throwable.getMessage());
                    mIView.showNetworkError();
                }
            }
        }));
    }
}
